# -*- coding:utf-8 -*-
import json

# import util

# from util import ReplaceSelect
# import HelloWorld.util as util
# from HelloWorld.util import ReplaceSelect
# import HelloWorld.ReplaceSelect as ReplaceSelect
# from  HelloWorld.ReplaceSelect import ReplaceSelect
from  HelloWorld.util.ReplaceSelect import ReplaceSelect
# import HelloWorld.util.ReplaceSelect as ReplaceSelect
# util.Re 
# util. 
from django.http import HttpResponse
 # index的 views.py
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
import HelloWorld.models as m
# https://www.cnblogs.com/meloncodezhang/p/11752602.html
def hello(request):
    return HttpResponse("Hello world ! ")

#!/usr/bin/python3

import  pymysql


# https://acc15t4bm5.feishu.cn/docx/doxcnypmsvSDDWAXbMjBoRTBWVf
@csrf_exempt
def params_post(request):
    print("request post params_post")
    print(request)
    if request.method=='GET':
        return render(request,'post.html')
    else:

        print( "request.POST", request.POST)
        username=request.POST.get('username','')
        password=request.POST.get('password','')
        return HttpResponse('username='+username+"&password="+password)
import  pymongo
from django.http import JsonResponse
from .models import Blog
from django.core import serializers
from . import util
# from . import PyMysqlMeta
# from . import PyMysql
# import
# api_view
from rest_framework.decorators import api_view
#
#
# DATABASES_json_file_name=r"/home/app/private-conf/DATABASES.json"
# mongodb_conf_file=rf"D:\home\app\private-conf\mongodb.json"
# mongodb_conf_file=rf"/home/app/private-conf\mongodb.json"
mongodb_conf_file=rf"/home/app/private-conf/mongodb.json"

from django.http import HttpResponse
from django.core import serializers


# import json_util
import HelloWorld.util. json_util as json_util
from pymongo import MongoClient
# MongoClient django 查询 返回 json
# Client = MongoClient()
# mango_uri = 'mongodb://%s:%s' % ("47.93.194.180", 27017)
# D:\home\app\private-conf\mongodb.json
conf=json_util.file_path_to_dict(mongodb_conf_file)
# mango_uri = 'mongodb://%s:%s' % ("localhost", 27017)
# mongodb://root:*****@43.142.150.223:27017/?authSource=admin&readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false
# mango_uri = f'mongodb://{conf["username"]}:{conf["password"]}@{conf["host"]}:27017/job'
mango_uri = f'mongodb://{conf["username"]}:{conf["password"]}@{conf["host"]}:27017/?authSource=admin&readPreference=primary&appname=MongoDB%20Compass&directConnection=true&ssl=false'

# String uri =“mongodb://username:password@127.0.0.1:27017/dbname”;


# mango_uri  有密码
# json_util


conn = MongoClient(mango_uri)  # 创建链接
# db = conn.coder  # 连接coder数据库
# ModuleNotFoundError: No module named 'pymongo'
# print("db coder")
# print(db)
# pymongo 创建一个 collection
# client=conn["coder"]
# db=conn["coder"]
# mydb = conn["mydatabase"]
mydb = conn["job"]
mycol = mydb["job"]
job_collection = mydb["job"]
# mycol = mydb["customers"]
print("mycol",mycol)
# db.test.insert( { "item" : "card", "qty" : 15 })

from rest_framework.response import Response

from bson import ObjectId
from bson import json_util
import json

class JSONEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, ObjectId):
            return str(o)
        return json.JSONEncoder.default(self, o)

# res = json.dumps(DBResultMessage, cls=JSONEncoder)


def to_json(data):
    return json.dumps(data,ensure_ascii=False)

def mongo_to_json(mongo_data):
    data =json_util.dumps(mongo_data,ensure_ascii=False)
    # data=x
    # #  json.loads(data)
    data=json.loads(data)
    return data

import file_util
print("file_util.decimal_places")
print(file_util.decimal_places)

@api_view(['GET', 'POST'])
def getDatas(request):
    
    print("request",request)
    x = mycol.find_one()

    print("x mycol.find_one()")
    print(x)
    # data = json.dumps(x, cls=JSONEncoder)
    # data=util.modelResToJsonObj(data)
    # data =json_util.dumps(x,ensure_ascii=False)
    # # data=x
    # # #  json.loads(data)
    # data=json.loads(data)
    data=mongo_to_json(x)
    # data=to_json(x)
    # TypeError: Object of type ObjectId is not JSON serializable
    # return HttpResponse(
    #             json.dumps({"rep" : rep,"status" : status},ensure_ascii=False),
    #             content_type="application/json,charset=utf-8"
    #         )
    # data = serializers.serialize('json', x)
    # 'str' object has no attribute '_meta'
    # return HttpResponse(json_data, content_type="application/json")

    res={
        "code":200,
        "data":data,
        "msg":"ok"
    }
    # data["data"] = json.loads(ajax_testvalue)

    return JsonResponse(res)
    # return Response(res)


@api_view(['GET', 'POST'])
def find_one_by_jobTitle(request):
    if request.method==k.POST:
        post_data=get_post_data(request)
        jobTitle=post_data.get("jobTitle",'实施实习生')
        print("request",request)
        x = job_collection.find_one({'jobTitle':jobTitle})

        print("x mycol.find_one()")
        print(x)

        data=mongo_to_json(x)

        res={
            "code":200,
            "data":data,
            "msg":"ok"
        }

        return JsonResponse(res)

# x = mycol.find_one()

# print(x)
# from bson import json_util
# api_view

def api_view_get(request):
    if request.method=='GET':
        x = mycol.find_one()

        print("x mycol.find_one()")
        print(x)
        # dataJson=json.loads(x)
        # print("dataJson mycol.find_one()")
        # print(dataJson)
        # data=json_util.dumps(x)
        data=x
        res={
            "code":200,
            "data":data,
            "msg":"ok"
        }
        # data["data"] = json.loads(ajax_testvalue)

        return JsonResponse(res)

def find_one(request):
    if request.method=='GET':
        x = mycol.find_one()

        print("x mycol.find_one()")
        print(x)
        # dataJson=json.loads(x)
        # print("dataJson mycol.find_one()")
        # print(dataJson)
        # data=json_util.dumps(x)
        data=x
        # TypeError: Object of type ObjectId is not JSON serializable
        res={
            "code":200,
            "data":data,
            "msg":"ok"
        }
        # data["data"] = json.loads(ajax_testvalue)

        return JsonResponse(res)
        # return res
class k:
    POST='POST'
    GET='GET'


def get_post_data(request):
    return json.loads(request.body)

@csrf_exempt
# http://127.0.0.1:8000/ReplaceSelectTest
def ReplaceSelectTest(request):
    # util.Re 
    # rs = ReplaceSelect([5, 19, 25, 45, 30, 24, 15, 60, 16, 27, 1], 5)
    # rs.run()
    # POs 

    if request.method=='GET':
        rs = ReplaceSelect([5, 19, 25, 45, 30, 24, 15, 60, 16, 27, 1], 5)
        res_list=rs.run()
        # x = mycol.find_one()

        # print("x mycol.find_one()")
        # print(x)
        # # dataJson=json.loads(x)
        # # print("dataJson mycol.find_one()")
        # # print(dataJson)
        # # data=json_util.dumps(x)
        # data=x
        # json_util.CANONICAL_JSON_OPTIONS
        print("res_list")
        print(res_list)
        data=mongo_to_json(res_list)
        # data=to_json(res_list)
        # data=serializers.serialize('json', res_list)
        # # django 返回 json 是unicode 
        res={
            "code":200,
            "data":data,
            "msg":"ok"
        }
        # data["data"] = json.loads(ajax_testvalue)

        # return JsonResponse(res)
        # return JsonResponse(res_list,encoder=JSONEncoder)
        return JsonResponse(res)
        # return res
    if request.method==k.POST:
        print("request.POST")
        print(request.POST)
        body=request.body
        print("body")
        print(body)
        # input_lst=request.POST.get('input_lst')
        # memo_len=request.POST.get('memo_len')
        body=get_post_data(request)
        input_lst=body.get('input_lst')
        memo_len=body.get('memo_len')
        # json.loads(request.body)
        
        # memo_len: int
        print("input_lst")
        print(input_lst)
        print("memo_len")
        print(memo_len)

        rs = ReplaceSelect(input_lst, memo_len)
        res_list=rs.run()
        # x = mycol.find_one()

        # print("x mycol.find_one()")
        # print(x)
        # # dataJson=json.loads(x)
        # # print("dataJson mycol.find_one()")
        # # print(dataJson)
        # # data=json_util.dumps(x)
        # data=x
        # json_util.CANONICAL_JSON_OPTIONS
        print("res_list")
        print(res_list)
        data=mongo_to_json(res_list)
        # data=to_json(res_list)
        # data=serializers.serialize('json', res_list)
        # # django 返回 json 是unicode 
        res={
            "code":200,
            "data":data,
            "msg":"ok"
        }
        # data["data"] = json.loads(ajax_testvalue)

        # return JsonResponse(res)
        # return JsonResponse(res_list,encoder=JSONEncoder)
        return JsonResponse(res)

def coder_insert(request):

    if request.method=='GET':
        print("mydb.list_collection_names()")
        print(mydb.list_collection_names())
        # conn.coder  .insert( { "item" : "card", "qty" : 15 })
        # db["coder"].insert( { "item" : "card", "qty" : 15 })
        # mycol.insert( { "item" : "card", "qty" : 15 })
        mydict = { "name": "Bill", "address": "Highway 37" }

        x = mycol.insert_one(mydict)
        print("x",x)
        print("x.inserted_id")
        print(x.inserted_id)

        mylist = [
        { "name": "Amy", "address": "Apple st 652"},
        { "name": "Hannah", "address": "Mountain 21"},
        { "name": "Michael", "address": "Valley 345"},
        { "name": "Sandy", "address": "Ocean blvd 2"},
        { "name": "Betty", "address": "Green Grass 1"},
        { "name": "Richard", "address": "Sky st 331"},
        { "name": "Susan", "address": "One way 98"},
        { "name": "Vicky", "address": "Yellow Garden 2"},
        { "name": "Ben", "address": "Park Lane 38"},
        { "name": "William", "address": "Central st 954"},
        { "name": "Chuck", "address": "Main Road 989"},
        { "name": "Viola", "address": "Sideway 1633"}
        ]

        x = mycol.insert_many(mylist)

        # 打印被插入文档的  _id 值列表：
        print("x.inserted_ids 打印被插入文档的  _id 值列表：")
        print(x.inserted_ids)

        mylist = [
        { "_id": 1, "name": "John", "address": "Highway 37"},
        { "_id": 2, "name": "Peter", "address": "Lowstreet 27"},
        { "_id": 3, "name": "Amy", "address": "Apple st 652"},
        { "_id": 4, "name": "Hannah", "address": "Mountain 21"},
        { "_id": 5, "name": "Michael", "address": "Valley 345"},
        { "_id": 6, "name": "Sandy", "address": "Ocean blvd 2"},
        { "_id": 7, "name": "Betty", "address": "Green Grass 1"},
        { "_id": 8, "name": "Richard", "address": "Sky st 331"},
        { "_id": 9, "name": "Susan", "address": "One way 98"},
        { "_id": 10, "name": "Vicky", "address": "Yellow Garden 2"},
        { "_id": 11, "name": "Ben", "address": "Park Lane 38"},
        { "_id": 12, "name": "William", "address": "Central st 954"},
        { "_id": 13, "name": "Chuck", "address": "Main Road 989"},
        { "_id": 14, "name": "Viola", "address": "Sideway 1633"}
        ]

        x = mycol.insert_many(mylist)

        # 打印被插入文档的  _id 值列表：
        print("x.inserted_ids  打印被插入文档的  _id 值列表：")
        print(x.inserted_ids)

        id=request.GET.get('id',default='1')
        print( "request", request)
        # id=request.GET.id
        # request.GET.id

        id1Blog=Blog.objects.get(id=id)#按照指定条件查询
        # objects.get(id=id) 返回为json
        # print()

        print("id1Blog",id1Blog)
        ajax_testvalue=util.modelResToJsonObj(id1Blog)

        res={
            "code":200,
            "data":ajax_testvalue,
            "msg":"ok"
        }
        # data["data"] = json.loads(ajax_testvalue)

        return JsonResponse(res)

def themes_get_by_id(request):

    if request.method=='GET':
        # PyMysqlMeta.host
        # PyMysql.con()
        # pymysql.connect(host="loca",port=)
        # AttributeError: 'QueryDict' object has no attribute 'id'
        # django 获得 get 请求参数
        id=request.GET.get('id',default='1')
        print( "request", request)
        # id=request.GET.id
        # request.GET.id

        id1Blog=Blog.objects.get(id=id)#按照指定条件查询
        # objects.get(id=id) 返回为json
        # print()

        print("id1Blog",id1Blog)
        ajax_testvalue=util.modelResToJsonObj(id1Blog)
        # for val in id1Blog:
        #     print(val)
        # TypeError: Object of type Blog is not JSON serializable

        # ajax_testvalue = serializers.serialize("json",id1Blog)
        # ajax_testvalue = serializers.serialize("json",[id1Blog])
        # # BlogAll.fi
        # # ajax_testvalue=list(ajax_testvalue)
        #
        # # BlogAll = Blog.objects.filter(parea__isnull=True).values()
        # # print("BlogAll",BlogAll)
        # # ajax_testvalue=list(BlogAll)
        #
        # # TypeError: Object of type Blog is not JSON serializable
        # print("ajax_testvalue",ajax_testvalue)
        # # TypeError: 'Blog' object is not iterable
        # # ajax_testvalue=list(BlogAll)
        # # print("ajax_testvalue",ajax_testvalue)
        # ajax_testvalue=json.loads(ajax_testvalue)
        # print("ajax_testvalue",ajax_testvalue)
        # AttributeError: type object 'Blog' has no attribute 'bojects'
        res={
            "code":200,
            "data":ajax_testvalue,
            "msg":"ok"
        }
        # data["data"] = json.loads(ajax_testvalue)

        return JsonResponse(res)
        # return JsonResponse(res)
        # return render(request,'post.html')
    else:
        print( "request.POST", request.POST)
        username=request.POST.get('username','')
        password=request.POST.get('password','')
        res={
            "code":200,
            "data":request.POST,
            "msg":"ok"
        }
        return JsonResponse(res)

def get_fields(UserProfile):
    params = UserProfile._meta.fields
    for t in range(len(params)):
        print( params[t].name, params[t].verbose_name, params[t]._choices)
        #获取model中的choicesfield
#返回多个对象
# django 数据库 mysql
def themes(request):
    print("request post params_post")
    print(request)
    if request.method=='GET':

        # 打开数据库连接
        # db = pymysql.connect("localhost","testuser","test123","TESTDB")

        # # 使用 cursor() 方法创建一个游标对象
        # cursor = db.cursor()

        # # 使用 execute()  方法执行 SQL 查询
        # cursor.execute("SELECT VERSION()")

        # # 使用 fetchone() 方法获取单条数据.
        # data = cursor.fetchone()

        # print("Database version : %s " % data)#

        # # 关闭数据库连接
        # db.close()

        print( "request", request)
        # Blog.save()
        # Blog.lis
        # Blog.save()
        # print( "request.POST", request.POST)
        # https://blog.csdn.net/changfcqxhy/article/details/90757305
        # BlogAll=Blog.bojects.all()#查询所有数据
        # BlogAll=m.Blog.bojects.all()#查询所有数据
        # TypeError: Object of type QuerySet is not JSON serializable
        BlogAll=m.Blog.objects.all()
        # https://blog.csdn.net/a__int__/article/details/105163093
        # .objects.all() fields
        # BlogAll["fields"]
        # fields=BlogAll.fields
        # print( "fields", fields)
        values=BlogAll.values()
        print( "values", values)

        # AttributeError: type object 'Blog' has no attribute 'object'
        # id1Blog=Blog.object.get(id=1)#按照指定条件查询
        # id1Blog=m.Blog.object.get(id=1)#按照指定条件查询
        # id1Blog=m.Blog.objects.get(id=1)#按照指定条件查询
        # id1Blog=Blog.objects.get(id=1)#按照指定条件查询
        # # objects.get 返回json
        # print("id1Blog",id1Blog)

        # BlogAll.
        # m.Blog.objects.all().
        #查询所有数据
        # TypeError: Object of type QuerySet is not JSON serializable
        # python 后端 返回前端是字符串 不是json
        # print("BlogAll",BlogAll)
        ajax_testvalue = serializers.serialize("json",BlogAll)
        # BlogAll.fi
        # ajax_testvalue=list(ajax_testvalue)

        # BlogAll = Blog.objects.filter(parea__isnull=True).values()
        # print("BlogAll",BlogAll)
        # ajax_testvalue=list(BlogAll)

        # TypeError: Object of type Blog is not JSON serializable
        print("ajax_testvalue",ajax_testvalue)
        # ajax_testvalue=list(BlogAll)
        # print("ajax_testvalue",ajax_testvalue)
        ajax_testvalue=json.loads(ajax_testvalue)
        print("ajax_testvalue",ajax_testvalue)
        # AttributeError: type object 'Blog' has no attribute 'bojects'
        detail=values
        # themeName
        data={
            "detail":detail
        }
        res={
            "code":200,
            "data":data,
            "msg":"ok"
        }
        # data["data"] = json.loads(ajax_testvalue)

        return JsonResponse(res)
        # return JsonResponse(res)
        # return render(request,'post.html')
    else:
        print( "request.POST", request.POST)
        username=request.POST.get('username','')
        password=request.POST.get('password','')
        res={
            "code":200,
            "data":request.POST,
            "msg":"ok"
        }
        return JsonResponse(res)

@csrf_exempt
def testView(request):
    print("request post params_post")
    print(request)
    if request.method=='GET':
        return render(request,'post.html')
    else:
        print( "request.POST", request.POST)
        username=request.POST.get('username','')
        password=request.POST.get('password','')
        res={
            "code":200,
            "data":request.POST,
            "msg":"ok"
        }
        return JsonResponse(res)


import base64
from io import BytesIO

import requests
from django.http import JsonResponse
from django.shortcuts import render
import markdown
# Create your views here.
# import .models as m


#     "INSTALLED_APPS." % (module, name)
# RuntimeError: Model class HelloWorld.models.Blog doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
# import models as m
# import blog.models as m
import pandas as pd
import matplotlib
from matplotlib import pyplot as plt
#   File "D:\proj\python\hello\HelloWorld\HelloWorld\views.py", line 53, in <module>
#     import models as m
# ModuleNotFoundError: No module named 'models'

# 查询
# models.UserInfo.objects.all()
# models.UserInfo.objects.all().values('user')    #只取user列
# models.UserInfo.objects.all().values_list('id','user')    #取出id和user列，并生成一个列表
# models.UserInfo.objects.get(id=1)
# models.UserInfo.objects.get(user='yangmv')

# 增
# models.UserInfo.objects.create(user='yangmv',pwd='123456')
# 或者
# obj = models.UserInfo(user='yangmv',pwd='123456')
# obj.save()
# 或者
# dic = {'user':'yangmv','pwd':'123456'}
# models.UserInfo.objects.create(**dic)

# 删
# models.UserInfo.objects.filter(id=1).delete()

# 改
# models.UserInfo.objects.filter(user='yangmv').update(pwd='520')
# 或者
# obj = models.UserInfo.objects.get(user='yangmv')
# obj.pwd = '520'
# obj.save()
def index(request):
    i = request.GET.get('id')
    # http://localhost:8001/index?id=1
    # postdata = request.POST['id']
    blog = m.Blog.objects.get(id=i)
    blog_content = markdown.markdown(blog.content)
    spider = m.Spider.objects.all().values()
    plot_data = showMatplot(pd.DataFrame(spider))
    imb = base64.b64encode(plot_data)  # 对plot_data进行编码
    ims = imb.decode()
    imd = "data:image/png;base64," + ims
    return render(request, "blog.html", {'blog': blog, 'blog_content': blog_content, 'img': imd})


def indexes(request):
    blogs = m.Blog.objects.all()
    return render(request, "blogs.html", {'blogs': blogs})

#图标嵌入django页面
def showMatplot(df):
    plt.figure(figsize=(12, 10))  # 设置图像大小
    plt.subplot(212)  # 布局两行1列的第二个
    col = ['date', 'open', 'close', 'height', 'low', 'updownd', 'count']
    df = df.astype(
        {'open': 'float', 'close': 'float', 'height': 'float', 'low': 'float', 'updownd': 'float', 'count': 'float'})
    df = pd.DataFrame(df, columns=col)
    # print(df)
    # print("*" * 40)
    # # corr只能对数值型数据进行相关度计算
    # print(df.corr())
    x = pd.to_datetime(df['date'])
    y1 = df["open"]
    y2 = df["close"]
    y3 = df["height"]
    y4 = df["low"]

    plt.plot(x, y1, label='open', linestyle='-', c='black', linewidth=1)
    plt.plot(x, y2, label='close', linestyle='--', c='r', linewidth=1)
    plt.plot(x, y3, label='height', linestyle=':', c='g', linewidth=1)
    plt.plot(x, y4, label='low', linestyle='-.', c='b', linewidth=1)
    plt.legend()
    plt.subplot(211)  # 布局两行一列第一个
    df1 = df.sort_values(by='date', ascending=True)
    # df1 = df1.iloc[30:]
    col = ["date", "close"]
    close = pd.DataFrame(df1, columns=col)

    data = cal_macd_system(close, 12, 26, 9)
    x = pd.to_datetime(data['date'])
    # y = pd.to_numeric(data["close"])
    y1 = data["macd"]
    # plt.plot(x, y, label="k")
    plt.plot(x, y1, label="macd")
    plt.title("某公司股票", fontproperties=zhfont1, fontsize=15)
    plt.legend()
    buffer = BytesIO()
    plt.savefig(buffer)
    plot_data = buffer.getvalue()
    return plot_data

#下载字体放到项目目录中，解决图标中文显示
zhfont1 = matplotlib.font_manager.FontProperties(fname="FZSTK.TTF")

#macd指标算法
def cal_macd_system(data, short_, long_, m):
    '''
    data是包含高开低收成交量的标准dataframe
    short_,long_,m分别是macd的三个参数
    返回值是包含原始数据和diff,dea,macd三个列的dataframe
    '''
    data['diff'] = data['close'].ewm(adjust=False, alpha=2 / (short_ + 1), ignore_na=True).mean() - \
                   data['close'].ewm(adjust=False, alpha=2 / (long_ + 1), ignore_na=True).mean()
    data['dea'] = data['diff'].ewm(adjust=False, alpha=2 / (m + 1), ignore_na=True).mean()
    data['macd'] = 2 * (data['diff'] - data['dea'])
    return data

#绘制macd指标曲线
def macdview(df):
    df1 = df.sort_values(by='date', ascending=True)
    # df1 = df1.iloc[30:]
    col = ["date", "close"]
    close = pd.DataFrame(df1, columns=col)

    data = cal_macd_system(close, 12, 26, 9)
    x = pd.to_datetime(data['date'])
    # y = pd.to_numeric(data["close"])
    y1 = data["macd"]
    # plt.plot(x, y, label="k")
    plt.plot(x, y1, label="macd")
    plt.title("某公司股票", fontproperties=zhfont1, fontsize=15)
    # plt.legend()
    buffer = BytesIO()
    buffer.flush()
    plt.savefig(buffer)
    plot_data = buffer.getvalue()
    buffer.close()
    return plot_data

#在线启动爬虫
def start_scrapy(request):
    # 获取页面传参，要区分请求类型是POST还是GET，不同请求用不同的方法接收参数
    year = request.POST.get('year')
    jd = request.POST.get('jd')
    url = 'http://127.0.0.1:6800/schedule.json'
    # spider是执行scrapy list返回的名称,参数问题：除了内置key的参数外如project,spider等，其他参数均由爬虫初始化函数的kwargs接收
    # 同时jobid也有kwargs接收，**kwargs是接收字典型的参数，带有key值的
    data = {'project': 'pachong', 'spider': 'pachong_spider', 'year': year, 'jd': jd}
    print(requests.post(url=url, data=data))
    return JsonResponse({'result': 'ok'})
